package co.yixiang.modules.shop.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.shop.domain.YxMaterial;
import co.yixiang.modules.shop.service.YxMaterialService;
import co.yixiang.modules.shop.service.dto.YsMaterialDTO;
import co.yixiang.modules.shop.service.dto.YxMaterialDto;
import co.yixiang.modules.shop.service.dto.YxMaterialQueryCriteria;
import co.yixiang.modules.shop.service.mapper.MaterialMapper;
import co.yixiang.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxMaterialServiceImpl extends BaseServiceImpl<MaterialMapper, YxMaterial> implements YxMaterialService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxMaterialQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxMaterial> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxMaterialDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxMaterial> queryAll(YxMaterialQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxMaterial.class, criteria));
    }

    @Override
    public YsMaterialDTO getDTOById(String id) {
        YxMaterial item = getById(id);
        YsMaterialDTO dto = new YsMaterialDTO();
        dto.setId(item.getId());
        dto.setCreateTime(item.getCreateTime());
        dto.setCreateId(item.getCreateId());
        dto.setDownloads(0);
        dto.setType(item.getType());
        dto.setUpdateTime(item.getUpdateTime());
        dto.setGroupId(item.getGroupId());
        dto.setGroupName("分组");
        dto.setCollections(0);
        dto.setName(item.getName());
        dto.setUrl(item.getUrl());
        dto.setShares(0);
        dto.setIsCollected(0);
        dto.setRecommendList(new ArrayList<>());
        return dto;
    }

    @Override
    public List<YsMaterialDTO> getList(String type, String groupId, int page, int limit) {
        // 校验分页参数
        if (page <= 0 || limit <= 0) {
            throw new IllegalArgumentException("分页参数 page 和 limit 必须大于 0");
        }

        // 设置分页参数
        Pageable pageable = PageRequest.of(page - 1, limit);

        // 创建查询条件对象并设置 groupId
        YxMaterialQueryCriteria criteria = new YxMaterialQueryCriteria();
        if (StringUtils.isNotEmpty(groupId))
            criteria.setGroupId(groupId); // 假设 YxMaterialQueryCriteria 有一个 setGroupId 方法

        if (StringUtils.isNotEmpty(type))
            criteria.setType(type);

        // 查询所有数据并进行分页
        List<YxMaterial> list = queryAll(criteria); // 使用带条件的查询方法
        PageInfo<YxMaterial> pageInfo = new PageInfo<>(list); // 使用 PageInfo 包装结果

        // 返回分页结果中的数据列表
        List<YsMaterialDTO> reList = new ArrayList<>();

        pageInfo.getList().forEach(item -> reList.add(generator.convert(item, YsMaterialDTO.class)));

        return reList;
    }

}
